package com.houtai.service.user.info;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.RetInfo;
import com.houtai.bean.TdUserInfo;
import com.houtai.bean.dto.user.info.TdUserInfoListDto;
import com.houtai.common.util.LogUtils;
import com.houtai.dao.TdUserInfoDao;
import com.houtai.dao.TsJzAdminOperationDao;
import com.houtai.dao.UserDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserInfoService {
    private static Logger logger = Logger.getLogger(UserInfoService.class);

    @Autowired
    private TdUserInfoDao tdUserInfoDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TsJzAdminOperationDao tsJzAdminOperationDao;
    /**
     * @Purpose:    搜索个人资料
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public RetInfo userInfoList(TdUserInfoListDto page, HttpServletRequest request) {
        String logInfo =  ":userInfoList:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {

            PageHelper.startPage(page.getCurrent_page(), page.getPage_size(), "info_id desc");
            List<TdUserInfo> lists = tdUserInfoDao.findTdUserInfoList(page);
            PageInfo<TdUserInfo> list = new PageInfo<>(lists);
            Map<String, Object> map = new HashMap<>();
            map.put("list", list.getList());
            map.put("total_page", list.getPages());
            map.put("total_record", list.getTotal());
            map.put("current_page", list.getPageNum());
            map.put("page_size", list.getPageSize());
            map.put("real_name", page.getReal_name());
            map.put("state", page.getState());
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
            LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"查找个人资料" , request);


        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("json数据错误");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    /**
     * @Purpose   审核个人资料
     * @param     tdUserInfo
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public synchronized RetInfo auditTdUserInfo(TdUserInfo tdUserInfo, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":auditTdUserInfo:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            TdUserInfo detail = tdUserInfoDao.findTdUserInfo(tdUserInfo.getInfo_id());
            if (detail != null && detail.getState() == 1) {
                String result;
                detail.setState(tdUserInfo.getState());

                if (detail.getState() == 2) {
                    detail.setAudit_remark("审核通过");
                    result = "审核通过";
                    //更新用户认证结果 昵称
                    userDao.updateUserNick(detail.getNick_name(), detail.getUser_id());
                } else {
                    detail.setAudit_remark(tdUserInfo.getAudit_remark());
                    result = "审核不通过 原因：" + tdUserInfo.getAudit_remark();
                }

                tdUserInfoDao.updateTdUserInfo(detail);
                retInfo.setMark("0");
                retInfo.setTip("成功.");
                LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"审核个人资料,id:" + tdUserInfo.getInfo_id()
                        + "结果：" + result,request);
            } else {
                retInfo.setMark("1");
                retInfo.setTip("不可审核.");
            }


        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
